%%%-------------------------------------------------------------------
%% @doc mysqlboy_test public API
%% @end
%%%-------------------------------------------------------------------

-module(mysqlboy_test).

-include_lib("eunit/include/eunit.hrl").

-export([run/0]).

run() ->
    {ok, _} = application:ensure_all_started(mysqlboy),
    ?debugMsg("-----------------------------------------------------------------------------"),
    run1(),
    timer:sleep(1000),
    ?debugMsg("-----------------------------------------------------------------------------"),
    run2(),
    application:stop(mysqlboy),
    ok.

%% -----------------------------------------------------------------------------
run1() ->
    ?debugMsg("mysqlboy test begin. run1"),
    
    Name    = "dddd01",
    LV      = 11,
    Time    = erlang:system_time(second),

    Value = [Name, LV, Time],
    async_worker(db1, <<"insert into test1 value(null, ?, ?, ?);">>, Value),
    async_worker(db1, <<"insert into test1 value(null, ?, ?, ?);">>, Value),
    async_worker(db1, <<"insert into test1 value(null, ?, ?, ?);">>, Value),
    async_worker(db1, <<"update test1 set lv = ? where id = ?;">>, [12, 1]),
    async_worker(db1, <<"replace into test1 value(null, ?, ?, ?);">>, Value),
    async_worker(db1, <<"select id, name, lv, from_unixtime(time) from test1 where id > ? order by time desc limit 10;">>, [0]),
    async_worker(db1, <<"select count(*) as amount from test1;">>, []),


    timer:sleep(100),
    ?debugFmt("mysqlboy db1 status: ~p.", [gen_server:call(db1, status)]),
    timer:sleep(2000),
    ?debugFmt("mysqlboy db1 status: ~p.", [gen_server:call(db1, status)]),

    ?debugMsg("mysqlboy hold timeout test"),
    P = mysqlboy:checkout(db1),
    ?debugFmt("mysqlboy db1 status: ~p.", [gen_server:call(db1, status)]),
    P ! timeout,
    mysqlboy:checkin(db1, P),
    ?debugFmt("mysqlboy db1 status: ~p.", [gen_server:call(db1, status)]),

    ?debugMsg("mysqlboy test end. run1"),
    ok.

%% internal functions
async_worker(DB, Query, Params) ->
    spawn(fun() -> ?debugFmt("db1: ~p.", [query(DB, Query, Params)]) end).



query(DB, Query, Params) ->
    Worker = mysqlboy:checkout(DB),
    Result = mysqlboy:query(Worker, Query, Params),
    mysqlboy:checkin(DB, Worker),
    Result.

%% -----------------------------------------------------------------------------
run2() ->
    ?debugMsg("mysqlboy test begin. run2"),

    % M1 = #{name => map1, lv => 13},
    % M2 = #{name => "map1", lv => 13},
    % M3 = #{name => <<"map1">>, lv => 13},
    % M4 = #{"name" => map1, lv => 13},
    % M5 = #{<<"name">> => map1, lv => 13},

    % mysqlboy:pack_insert(M1) -> (name,lv) values ("map1",13)
    % mysqlboy:pack_insert(M2) -> (name,lv) values ("map1",13)
    % mysqlboy:pack_insert(M3) -> (name,lv) values ("map1",13)
    % mysqlboy:pack_insert(M4) -> (lv,name) values (13,"map1")
    % mysqlboy:pack_insert(M5) -> (lv,name) values (13,"map1")

    % mysqlboy:pack_selete(M1) ->  where name="map1" and lv=13
    % mysqlboy:pack_selete(M2) ->  where name="map1" and lv=13
    % mysqlboy:pack_selete(M3) ->  where name="map1" and lv=13
    % mysqlboy:pack_selete(M4) ->  where lv=13 and name="map1"
    % mysqlboy:pack_selete(M5) ->  where lv=13 and name="map1"

    % mysqlboy:pack_update(M1) ->  set name="map1", lv=13
    % mysqlboy:pack_update(M2) ->  set name="map1", lv=13
    % mysqlboy:pack_update(M3) ->  set name="map1", lv=13
    % mysqlboy:pack_update(M4) ->  set lv=13, name="map1"
    % mysqlboy:pack_update(M5) ->  set lv=13, name="map1"

    Worker = mysqlboy:checkout(db1),
    Table  = test1,

    D1 = #{name => easyps, lv => max},
    D2 = #{time => erlang:system_time(second)},
    ?debugFmt("select   : ~p", [ select(Worker, Table, D1) ]),
    ?debugFmt("insert   : ~p", [ mysqlboy:insert(Worker, Table, D1) ]),
    R  = select(Worker, Table, D1),
    ID = hd(hd(R)),
    D3 = #{id => ID},
    D4 = #{id => ID, name => easyps, time => erlang:system_time(second)},
    ?debugFmt("select   : ~p", [ R ]),
    ?debugFmt("id       : ~p", [ ID ]),
    ?debugFmt("update   : ~p", [ mysqlboy:update(Worker, Table, D2, D3) ]),
    ?debugFmt("select   : ~p", [ select(Worker, Table, D3) ]),
    ?debugFmt("replace  : ~p", [ mysqlboy:replace(Worker, Table, D4) ]),
    ?debugFmt("select   : ~p", [ select(Worker, Table, D3) ]),
    ?debugFmt("delete   : ~p", [ mysqlboy:delete(Worker, Table, D3) ]),
    ?debugFmt("select   : ~p", [ select(Worker, Table, D3) ]),

    ?debugMsg("mysqlboy test end. run2"),
    ok.

select(Worker, Table, Data) ->
    {ok, _, D} = mysqlboy:select(Worker, Table, Data),
    D.

